<?php
require_once 'BaseController.php';
require_once APPLICATION_PATH.'/models/Slide.php';

class SlideController extends BaseController{

    public function slideAction(){
        $this->view->imgpath = PUBLIC_RES_PATH.'/imgs/slide/';
        $tblslide = new Slide();
        $this->view->slides = $tblslide->getSlides();
    }

    public function editslideAction(){
        $this->view->imgpath = PUBLIC_RES_PATH.'/imgs/slide/';
        $id = $this->getCheckedId();
        $tblslide = new Slide();
        $this->view->slide = $tblslide->getSlideById($id);
        $this->view->title = '修改幻灯片';
        if($this->view->slide == null){
            //没有找到
            header('Location:'.ADMIN_PATH.'/Slide/slide');
            exit;
        }
        $this->view->act = 'edit';
    }

    public function addslideAction(){
        $this->view->imgpath = PUBLIC_RES_PATH.'/imgs/slide/';
        $this->view->title = '添加幻灯片';
        $this->view->slide = Array(
            'title' => '',
            'order' => '',
            'link' => '',
            'img' => 'default.jpg',
        );
        $this->view->act = 'add';
        $this->render('editslide');
    }

    public function delslideAction(){
        $id = $this->getCheckedId();
        $tblslide = new Slide();
        if($tblslide->deleteSlide($id)<1){
            //删除失败
        }else {
            //删除成功
        }
        header('Location:'.ADMIN_PATH.'/Slide/slide');
        exit;
    }

    public function doupdateAction(){
        $act = $this->_getParam('act');
        $tblslide = new Slide();
        $set = Array(
            'title' => $this->_getParam('title'),
            'order' => $this->_getParam('order'),
            'link' => $this->_getParam('link'),
        );
        $filename = $this->uploadImg();

        if($act == 'edit'){
            if($filename != null){
                $set['img'] = $filename;
            }
            $id = $this->getCheckedId();
            $affected = $tblslide->updateSlide($id, $set);
            if($affected == 0){
                echo '更新无效：您没有对内容进行任何更改！';
            }else if($affected == 1){
                echo '更新成功！';
            }else{
                echo '更新失败！请认真检查您所填写的内容！';
            }
        }
        else if($act == 'add'){
            if($filename == null){
                //图片上传失败
                echo '<script>alert("图片上传失败！");</script>';
                exit;
            }
            $set['img'] = $filename;
            if($tblslide->addSlide($set)<1){
                //添加失败
            }else {
                //添加成功
            }
        }
        else{}
        header('Location:'.ADMIN_PATH.'/Slide/slide');
        exit;
    }

    private function getCheckedId(){
        $id = $this->_getParam('id');
        if(!is_numeric($id)){
            //参数不合法
            header('Location:'.ADMIN_PATH.'/Slide/slide');
            exit;
        }
        return $id;
    }

    private function uploadImg(){
        $filename = date('Ymd',time()).mt_rand().'.jpg';
        $upload = new Zend_File_Transfer(); //实例化上传类
        $updir = APPLICATION_PATH.'/../public/imgs/slide/';

        $upload->addValidator('Size', false, 2048 * 1024);  //大小限制2m
        $upload->addValidator('Extension', false, 'jpg');   //文件格式限制

        if (!$upload->isValid()) {
            return null;
        }

        $upload->setDestination($updir);
        $upload->addFilter('Rename', array('target' => $filename, 'overwrite' => false));
        if ($upload->receive()) {
            return $filename;
        }

        return null;
    }
}
